package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpProductClassify;
import com.erp.gather.domain.bo.ErpProductClassifyBo;
import com.erp.gather.domain.vo.ErpProductClassifyVo;
import com.erp.gather.mapper.ErpProductClassifyMapper;
import com.erp.gather.service.IErpProductClassifyService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFYService业务层处理
 *
 * @author erp
 * @date 2023-03-07
 */
@RequiredArgsConstructor
@Service
public class ErpProductClassifyServiceImpl implements IErpProductClassifyService {

    private final ErpProductClassifyMapper baseMapper;

    /**
     * 查询商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFY
     */
    @Override
    public ErpProductClassifyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFY列表
     */
    @Override
    public TableDataInfo<ErpProductClassifyVo> queryPageList(ErpProductClassifyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpProductClassify> lqw = buildQueryWrapper(bo);
        Page<ErpProductClassifyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFY列表
     */
    @Override
    public List<ErpProductClassifyVo> queryList(ErpProductClassifyBo bo) {
        LambdaQueryWrapper<ErpProductClassify> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpProductClassify> buildQueryWrapper(ErpProductClassifyBo bo) {
        LambdaQueryWrapper<ErpProductClassify> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), ErpProductClassify::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpProductClassify::getName, bo.getName());
        lqw.eq(bo.getCount() != null, ErpProductClassify::getCount, bo.getCount());
        return lqw;
    }

    /**
     * 新增商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFY
     */
    @Override
    public Boolean insertByBo(ErpProductClassifyBo bo) {
        ErpProductClassify add = BeanUtil.toBean(bo, ErpProductClassify.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFY
     */
    @Override
    public Boolean updateByBo(ErpProductClassifyBo bo) {
        ErpProductClassify update = BeanUtil.toBean(bo, ErpProductClassify.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpProductClassify entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品分类标签;参考数据字典ERP_PRODUCT_CLASSIFY
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
